package com.twu.biblioteca;

import java.io.*;
import java.security.MessageDigest;
import java.util.*;


public class BibliotecaApp {

    private HashMap<String, List> db;
    private HashMap<String, String> currentUser = new HashMap<String, String>();

    public static void main(String[] args) {
        BibliotecaApp app  = new BibliotecaApp();
        System.out.println(app.welcomeMsg());
        System.out.println(app.menuOption());
        while (true)
            System.out.println(app.acceptCommand());
    }

    public BibliotecaApp(){
        db = Utils.loadYamlDB();
    }
    public String acceptCommand() {

        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String output = "";
        try {
            String option = reader.readLine();
            switch (option.charAt(0)){
                case 'b': output = listOfAllBooks(); break;
                case 'r':
                    if(promoteLogin(reader)){
                        System.out.println("input name of book");
                        output = reserve(reader.readLine());
                    }
                    break;
                case 'd': output = membershipDetail();break;
                case 'm': output = listMovies();break;
                case 's': if(promoteLogin(reader)) System.out.print("Loged in");break;
                default: output = "Select a valid option!!";
            }
        } catch (IOException e) {
             System.out.println("IOERROR");
        }
        return output;
    }

    public String welcomeMsg() {
        return "**======= Welcome at Bibilioteca =======**\n**------- input the command in the menu below -------**";
    }

    public String menuOption() {
        return Utils.prettyTableFormater(db.get("menu"), Collections.<String>emptyList());
    }

    public String listOfAllBooks() {
        List<HashMap> booklist = db.get("book");
        return Utils.prettyTableFormater(booklist,Collections.<String>emptyList());
    }

    public String reserve(String name) {
        if(!Utils.findIn(db.get("book"), "Title", name).isEmpty()){
            return "Thank You! Enjoy the book.";
        }
        return "Sorry we don't have that book yet.";
    }

    public String membershipDetail() {
        if (!currentUser.isEmpty())
            return Utils.prettyTableFormater(Arrays.<HashMap>asList(currentUser),Arrays.asList("password"));
        return "Please talk to a Librarian. Thank you.";
    }

    public String listMovies(){
        List<HashMap> movielist = db.get("movie");
       return Utils.prettyTableFormater(movielist,Collections.<String>emptyList());
    }

    public Boolean login(String name, String password) {
        MessageDigest md = null;
        String md5password = null;
        try {
            md = MessageDigest.getInstance("MD5");
            byte md5hex[] =md.digest(password.getBytes());
            StringBuffer hexString = new StringBuffer();
            for (int i=0;i<md5hex.length;i++) {
                hexString.append(Integer.toHexString(0xFF & md5hex[i]));
            }
            md5password = hexString.toString();
        } catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

        List<HashMap> memberdata = db.get("member");

        currentUser = Utils.findIn(memberdata, "name", name);
        return md5password.equals(currentUser.get("password"));
    }

    public Boolean promoteLogin(BufferedReader reader) {
        System.out.println("username:");
        System.out.println("password:");
        try {
            return login(reader.readLine(),reader.readLine());
        } catch (IOException e) {

        }
        return false;
    }

}
